<template>
    <div id="myCard">
        <header class="header"><router-link to='/mine?type=3' class="addTrend"><i class="el-icon-arrow-left"></i></router-link>我的留言</header>
        <div class="wordlists">
            <scroller snappingHeight="10" class="scroller" :on-refresh="refresh" :on-infinite="infinite" ref="my_scroller">
                <div class="list" v-for="(item,index) in messageLists" v-if="item.uid != uid" :key="index">
                    <div class="wordlist">
                        <div class="wordlistL">
                            <div class="wordlistLU"><span class="qname">{{decodeURIComponent(item.crealname)}}</span>&nbsp;&nbsp;问&nbsp;&nbsp;<span class="mname">我</span><span>&nbsp;&nbsp;{{timeBetween(item.ddate)}}</span></div>
                            <div class="wordlistLD">{{item.content}}</div>
                        </div>
                        <div class="wordlistR" @click="toReply(decodeURIComponent(item.crealname),item.id,item.replyLists)">
                            <i class="iconfont icon-yiguanzhu"></i>
                            <span>回复</span>
                        </div>
                    </div>
                    <div class="replaylist" v-for="(v,k) in item.replyLists" :key="k">
                        <div class="wordlistL">
                            <div class="wordlistLU"><span class="qname">我</span>&nbsp;&nbsp;回复&nbsp;&nbsp;<span class="rname">{{decodeURIComponent(v.creplyrealname)!='undefined'?decodeURIComponent(v.creplyrealname):replyName}}</span><span>&nbsp;&nbsp;{{timeBetween(item.ddate)}}</span></div>
                            <div class="wordlistLD">{{v.content}}</div>
                        </div>
                    </div>
                </div>
                <div class="list" v-else :key="index">
                    <div class="wordlist">
                        <div class="wordlistL">
                            <div class="wordlistLU"><span class="qname">我</span>&nbsp;&nbsp;问&nbsp;&nbsp;<span class="mname">{{decodeURIComponent(item.ccardrealname)}}</span><span>&nbsp;&nbsp;{{timeBetween(item.ddate)}}</span></div>
                            <div class="wordlistLD">{{item.content}}</div>
                        </div>
                    </div>
                    <div class="replaylist" v-for="(v,k) in item.replyLists" :key="k">
                        <div class="wordlistL">
                            <div class="wordlistLU"><span class="qname">{{decodeURIComponent(v.crealname)!='undefined'?decodeURIComponent(v.crealname):replyName}}</span>&nbsp;&nbsp;回复&nbsp;&nbsp;<span class="rname">我</span><span>&nbsp;&nbsp;{{timeBetween(item.ddate)}}</span></div>
                            <div class="wordlistLD">{{v.content}}</div>
                        </div>
                    </div>
                </div>
            </scroller>
        </div>
        <footer v-if="replayAble">
            <el-input v-model="inputval" placeholder="请输入回复内容"></el-input>
            <el-button type="primary" @click="toGoReply">回复</el-button>
        </footer>
    </div>
</template>

<script>
import {messages,reply} from '../../../js/dataServe.js'
export default {
    data(){
        return {
            replyLists:[],//每一个询问下对应的回复
            replayAble:false,
            messageLists:[],
            inputval:'',
            ireplyid:'',
            replyName:'',
            uid:sessionStorage.uid,
            pageNo:1
        }
    },
    components:{},
    created(){
        // this.$store.commit('changeShowit',{val:false})
    },
    mounted(){
       
    },
    methods:{
        infinite(done) {

            if(sessionStorage.sid){
                let self = this;
                let responseData = messages(self.pageNo);
                responseData.then((res)=>{
                                        // alert(decodeURIComponent(res.msg))
                                        if(res.data && res.data.list.length>0){
                                           
                                            let lists = res.data.list;
                                            lists.forEach((item,index) => {
                                                if(!item.ireplyuid){
                                                    item.replyLists = [];
                                                    self.messageLists.push(item)
                                                }
                                            })
                                            lists.forEach((item,index) => {
                                                self.messageLists.forEach((v,k) => {
                                                    if(item.ireplyid == v.id){
                                                        v.replyLists.push(item)
                                                        // console.log(v)
                                                    }
                                                })
                                            })
                                            self.pageNo++;
                                            done();
                                            
                                        }else{
                                            self.$refs.my_scroller.finishInfinite(2);
                                        }
                                    })
            }
        },
        refresh(done) {
            done()
        },
        toReply(replayName,id,replyLists){
            this.replyName = replayName;
            this.replayAble = true;
            this.ireplyid = id;
            this.replyLists = replyLists;
        },
        toGoReply(){
            let self = this;
            let responseData = reply(self.inputval,self.ireplyid);
            responseData.then((res)=>{
                                        if(res.result==1){
                                            self.replyLists.push(res.data)
                                            self.$message({
                                                duration: 2000,
                                                message: res.msg,
                                                type: 'success'
                                            })
                                            this.replayAble = false;
                                        }
                                    })
        }
    }
}
</script>

<style scoped >
#myCard {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    width: 100%;
    height:100%;
    position: absolute;
    background: #eee;
    top: 0;
    left: 0;
}
.header{
    width:100%;
    height:3rem;
    line-height:3rem;
    background:#eee;
    color:#333;
    font-size:1.2rem;
    position: fixed;
    top: 0;
    z-index:5;
}
.header i{ color: #333;}
header a{
    position: absolute;
    left: 2%;
    color: #ffffff;
    width: 2rem;
}
.wordlistUp,.wordlistDown{
    height:1.5rem;
    line-height:1.5rem;
}
.wordlists{
    position: absolute;
    top: 3rem;
    bottom: 0;
    width: 100%;
}
.wordlist{
    background:#fff;
    padding:0.5rem 1rem;
    display:flex;
    justify-content: space-between;
    box-sizing: border-box;
}
.wordlistR{
    display: flex;
    flex-direction: column;
    justify-content: center;
}
.wordlistL{
    line-height: 2rem;
}
.wordlistLD{
    text-align: left;
    padding: 0 1rem;
    
}
.wordlistLU{
    text-align: left;
}
.qname{
    color: #1ACCAC;
}
.mname{
    color: #e6e6e6;
}
.rname{
    color:#c6c6c6;
}
.replaylist{
    padding: 0 1rem;
    box-sizing: border-box;
}
.list{ margin-bottom: 1rem; background: #fff;}
footer{
    display: flex;
    padding: 0.5rem 1rem;
    border-top: 1px solid #d6d6d6;
    position: fixed;
    width: 100%;
    box-sizing: border-box;
    bottom: 0;
    background:#ffffff;
}
</style>
